﻿using BCCommon;
using BCEntity.TMS.RedPacket.RedPacketReceive;
using Dapper;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;

namespace BCData.TMS.RedPacket.RedPacketReceive
{
    public class RedPacketReceiveData : IRedPacketReceiveData
    {
        private const string SQL_QUERY_VIEW = @" SELECT
		                                            A.RedPacketReceiveId,
		                                            A.RedPacketNumber,
		                                            A.UserId,
		                                            A.UserName,
		                                            A.RedPacketId,
		                                            A.RedPacketTypeId,
		                                            A.RedPacketType,
		                                            A.RedPacketAmount,
		                                            A.GrundlagenType,
		                                            A.RedPacketName,
		                                            A.AgentRecordId,
		                                            A.CreateTime,
                                                    A.ProvinceCode,
                                                    A.CityCode,
                                                    A.CountyCode
                                                FROM
                                                    redpacketreceive A ";

        private readonly IDatabaseContext database;
        public RedPacketReceiveData(IDatabaseContext database)
        {
            this.database = database;
        }

        public async Task<RedPacketReceiveEntity> GetEntityByRedPacketId(long redPacketId, long userId)
        {
            var sql = $" {SQL_QUERY_VIEW} WHERE A.RedPacketId =?RedPacketId AND A.UserId=?UserId";
            return await this.database.Slave.QuerySingleOrDefaultAsync<RedPacketReceiveEntity>(sql, new { redPacketId, userId });
        }

        public async Task<IEnumerable<RedPacketReceiveEntity>> GetList(long? redPacketTypeId, long? redPacketId, long? userId)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (redPacketTypeId.HasValue)
            {
                wheres.Add(" A.RedPacketTypeId= ?RedPacketTypeId ");
                parameters.Add("?RedPacketTypeId", redPacketTypeId.Value, DbType.Int64);
            }
            if (userId.HasValue)
            {
                wheres.Add(" A.UserId= ?UserId ");
                parameters.Add("?UserId", userId.Value, DbType.Int64);
            }
            if (redPacketId.HasValue)
            {
                wheres.Add(" A.RedPacketId= ?RedPacketId ");
                parameters.Add("?RedPacketId", redPacketId.Value, DbType.Int64);
            }

            string whereSql = string.Empty;
            if (wheres.Count > 0)
            {
                whereSql = $" WHERE {string.Join(" AND ", wheres)} ";
            }
            string sql = $"{SQL_QUERY_VIEW} {whereSql} GROUP BY A.RedPacketReceiveId Order By A.CreateTime DESC";
            return await this.database.Slave.QueryAsync<RedPacketReceiveEntity>(sql, parameters);
        }

        public async Task<RedPacketReceiveEntity> GetRedPacketNumber()
        {
            var sql = $" {SQL_QUERY_VIEW} ORDER BY A.RedPacketReceiveId DESC;";
            return await this.database.Slave.QueryFirstOrDefaultAsync<RedPacketReceiveEntity>(sql);
        }

        public async Task<RedPacketReceiveEntity> InsertAsync(RedPacketReceiveEntity entity)
        {
            return await this.database.Master.InsertAsync(entity, database.Transaction);
        }

        public async Task<(IEnumerable<RedPacketReceiveEntity>, int)> Query(long agentRecordId, long? redPacketId, long? userId,
            int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            wheres.Add(" A.AgentRecordId= ?AgentRecordId ");
            parameters.Add("?AgentRecordId", agentRecordId, DbType.Int64);
            if (userId.HasValue)
            {
                wheres.Add(" A.UserId= ?UserId ");
                parameters.Add("?UserId", userId.Value, DbType.Int64);
            }
            if (redPacketId.HasValue)
            {
                wheres.Add(" A.RedPacketId= ?RedPacketId ");
                parameters.Add("?RedPacketId", redPacketId.Value, DbType.Int64);
            }
            string whereSql = string.Empty;
            if (wheres.Count > 0)
            {
                whereSql = $"WHERE {string.Join(" AND ", wheres)} ";
            }
            var sqlCount = " SELECT COUNT(A.RedPacketReceiveId) FROM RedPacketReceive A ";
            string sql = $@"{SQL_QUERY_VIEW} {whereSql} GROUP BY A.RedPacketReceiveId Order By A.CreateTime DESC LIMIT {(pageIndex - 1) * pageSize}, {pageSize};
                    {sqlCount}{whereSql}";
            return await this.database.Slave.QueryPageableListAsync<RedPacketReceiveEntity>(sql, parameters);
        }

        public async Task<RedPacketReceiveEntity> UpdateAsync(RedPacketReceiveEntity entity)
        {
            return await this.database.Master.UpdateAsync(entity, database.Transaction);
        }
    }
}
